<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\tool\Result;
use think\facade\View;
use think\response\Json;
use app\common\model\News as NewsModel;

class News extends Base
{


    /**
     * get: 新闻列表
     * path: getList
     * method: getList
     * param: page - page
     */
    public function getList(): \think\Response
    {
        $param = $this->request->param();
        $map = ['delete_time' => 0, 'status' => 1, 'type'=>1];
        $list = NewsModel::getPageList($map, 'id,title,create_time', $param['page'], 20, true);
        if($list['code']>0)
        {
            return Result::success('获取成功',$list);
        }else{
            return Result::fail('获取失败');
        }
    }

    /**
     * get: 新闻详情
     * path: info
     * method: info
     * param: id - id
     */
    public function info()
    {
        $id = $this->request->param('id');
        $info = NewsModel::where('id', $id)->field('id,create_time,title,content,status,delete_time')->find();
        if (empty($info) || $info->status != 1 || $info->delete_time) {
            return json(['code' => -1, 'msg' => '记录不存在']);
        }
        $info->content = htmlspecialchars_decode($info->content);

        return Result::success('获取成功',$info);
    }

}

